/*******************************************************************************
 * Copyright (c) 2005, 2007 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package us.gaaoc.framework.dao;

import org.apache.log4j.Logger;
import org.hibernate.Query;

import us.gaaoc.framework.model.Attorney;
import us.gaaoc.framework.model.Organizations;
import us.gaaoc.framework.model.Person;

public class AttorneyDAOHibernate extends
GenericHibernateDAO<Attorney, String, AttorneyDAO> implements
AttorneyDAO {
	
	Logger log = Logger.getLogger(AttorneyDAOHibernate.class);
	
	public Attorney findAttorneyByBarID(String barID) {
		new Exception("findAttorneyByBarID is deprecated").printStackTrace();
		Query q = getSession().createQuery("from Attorney a where a.barID = ?");
		q.setString(0, barID);
		return (Attorney) q.uniqueResult();
	}
	
	@Deprecated
	public Attorney findAttorneyByPerson(Person person) {
		
		new UnsupportedOperationException("findAttorneyByPerson is deprecated").printStackTrace();
		LOG.info("Inside the findAttorneyByPerson Method ...");
		Query q = this.getSession().createQuery("from Attorney a where a.person.uuid = ?");
		q.setString(0, person.getUuid());	
		LOG.info("return of query  ...");
		
		//LOG.info("The name of the attorney is .."+((Attorney)q.uniqueResult()).getPerson().getLastName());
		//if(q!=null){
			
			return (Attorney)q.uniqueResult();
		/*}else{
			return null;
		}*/
		
	}
	
	public Attorney findAttorneyByPersonAndOrganization(Person person,Organizations c) {
		LOG.info("Inside the findAttorneyByPerson Method ...");
		Query q = this.getSession().createQuery("from Attorney a where a.person = ? and a.organization = ?");
		q.setString(0, person.getUuid());
		q.setString(0, c.getUuid());
		LOG.info("return of query  ...");
		
		//LOG.info("The name of the attorney is .."+((Attorney)q.uniqueResult()).getPerson().getLastName());
		//if(q!=null){
			
			return (Attorney)q.uniqueResult();
		/*}else{
			return null;
		}*/
		
	}



	public Person findAttorneyByBarIDAndOrgAsPerson(String barID,
			String courtUuid) {
		// TODO Auto-generated method stub
		Query q= this.getSession().createQuery("select a.person from Attorney a where a.barID = ? and a.organization = ?");
		q.setString(0,barID);
		q.setString(1, courtUuid);
		return (Person)q.uniqueResult();
	}


	@Override
	public Attorney getNullAttorney(Organizations organization) {
		// TODO Auto-generated method stub
		Query q = this.getSession().createQuery("from Attorney a where a.organization = ? and barID = '0'");
		q.setString(0,organization.getUuid());
		return (Attorney)q.uniqueResult();
	}


	@Override
	public String getBarID(Person person, us.gaaoc.framework.model.Court court) {
		Query q1 = this.getSession().createQuery("select o.organization from PersonOrganizations o where o.courtUuid = ? and o.person = ?");
		q1.setEntity(0,court);
		q1.setEntity(1,person);
		
		Organizations o = (Organizations) q1.uniqueResult();
		
		log.debug("Organization is " + o.getUuid());
		log.debug("Person is " + person.getUuid());
		log.debug("Court is " + court.getUuid());
		
		Query q = this.getSession().createQuery("from Attorney a where a.person = ? and a.organization = ?");
		q.setEntity(0, person);
		q.setEntity(1, o);
		Attorney a = (Attorney) q.uniqueResult();
		String barID = "";
		if (a != null)
		{
			barID = a.getBarID();
		}else{
			(new Exception("Person does not have a bar ID or is not an Attorney!")).printStackTrace();
		}	
		log.debug("Bar ID: is: " + barID);
		return barID;
	}


	@Override
	public Attorney findAttorneyByBarIDAndOrg(String barID, Organizations org) {
		Query q= this.getSession().createQuery("select a.person from Attorney a where a.barID = ? and a.organization = ?");
		q.setString(0,barID);
		q.setEntity(1, org);
		return (Attorney)q.uniqueResult();
	}
	
}
